<?php

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\common\model\AdminModel;
use think\Db;
use think\Exception;
use think\Request;
use think\Validate;

class Admin extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = $this->request->post();
        $page = !empty($data['page']) ? $data['page'] : 1;
        $pageSize = !empty($data['pageSize']) ? $data['pageSize'] : 10;
        $db = new AdminModel();
        $where = [];
        $list = $db->where($where)->field('id,username')->paginate($pageSize, false, ['page' => $page]);
        $total = $list->total();
        if (empty($total)) {
            return json(['code' => 1, 'msg' => '暂无数据', 'data' => $list]);
        } else {
            return json(['code' => 0, 'msg' => '数据已加载', 'data' => $list]);
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $this->request->post();
        //验证器
        //验证
        $validate = new \app\api\validate\Admin();
        $result = $validate->check($data);
        if (!$result) {
            return json(['code' => 0, 'message' => $validate->getError()]);
        }
        $db = new AdminModel();
        if (isset($data['id']) && !empty($data['id'])) {
            //修改
            $info = $db->where('id', $data['id'])->find();
            if (!empty($info)) {
                if (isset($data['password']) && !empty($data['password'])) {
                    if ($info['password'] === md5($data['password'])) {
                        $data['password'] = $info['password'];
                    } else {
                        $data['password'] = md5($data['password']);
                    }
                }

            } else {
                return json(['code' => 999, 'msg' => '数据不存在']);
            }

            $res = $db->save($data, ['id' => $data['id']]);
            return json(['code' => 999, 'msg' => '修改成功', 'data' => $res]);
        } else {
            $data['password'] = md5($data['password']);
            //新增
            $res = $db->save($data);
            return json(['code' => 999, 'msg' => '新增成功', 'data' => $res]);
        }
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @return \think\Response
     */
    public function delete()
    {
        //
        $data = $this->request->post();
        $id = $data['id'];
        if ($id == 1) {
            return json(['code' => 1, 'msg' => '想删管理员想啥呢！']);
        }
        Db::startTrans();
        try {
            Db::table('admin')->where('id', $data['id'])->delete();
            return json(['code' => 1, 'msg' => '删除success']);
            Db::commit();
        } catch (Exception $ex) {
            Db::rollback();
            return json($ex->getError(), 422);
        }

    }
}
